using System;
using NUnit.Framework;
using dnAnalytics.Math;
namespace dnAnalytics.UnitTests.Math
{
    [TestFixture]
    [Category("Managed")]
    public class ComplexMathTest
    {
        [Test]
        public void Conjugate()
        {
            Complex cd1 = new Complex(1.1, -2.2);
            Complex cd2 = new Complex(0, -2.2);
            Complex cd3 = new Complex(1.1, 0);
            Complex cd4 = new Complex(-1.1, 2.2);
            Assert.AreEqual(ComplexMath.Conjugate(cd1), new Complex(1.1, 2.2));
            Assert.AreEqual(ComplexMath.Conjugate(cd2), new Complex(0, 2.2));
            Assert.AreEqual(ComplexMath.Conjugate(cd3), new Complex(1.1, 0));
            Assert.AreEqual(ComplexMath.Conjugate(cd4), new Complex(-1.1, -2.2));
        }

        [Test]
        public void Max()
        {
            Complex cd1 = new Complex(1.1, -2.2);
            Complex cd2 = new Complex(0, -2.2);
            Complex cdt = ComplexMath.Max(cd1, cd2);
            Assert.AreEqual(cd1, cdt);
        }

        [Test]
        public void Norm()
        {
            Complex cd1 = new Complex(1.1, -2.2);
            Complex cd2 = new Complex(0, -2.2);
            Complex cd3 = new Complex(1.1, 0);
            Complex cd4 = new Complex(-1.1, 2.2);
            Assert.AreEqual(ComplexMath.Norm(cd1), 2.45967477524977, Constants.ACCEPTABLE_ERROR);
            Assert.AreEqual(ComplexMath.Norm(cd2), 2.2, Constants.ACCEPTABLE_ERROR);
            Assert.AreEqual(ComplexMath.Norm(cd3), 1.1, Constants.ACCEPTABLE_ERROR);
            Assert.AreEqual(ComplexMath.Norm(cd4), 2.45967477524977, Constants.ACCEPTABLE_ERROR);
        }

        [Test]
        public void Polar()
        {
            Complex cd1 = new Complex(1.1, -2.2);
            Complex cd2 = new Complex(0, -2.2);
            Complex cd3 = new Complex(1.1, 0);
            Complex cd4 = new Complex(-1.1, 2.2);
            Complex cdt = ComplexMath.Polar(cd1);
            Assert.AreEqual(cdt.Real, 2.45967477524977, Constants.ACCEPTABLE_ERROR);
            Assert.AreEqual(cdt.Imaginary, -1.10714871779409, Constants.ACCEPTABLE_ERROR);
            cdt = ComplexMath.Polar(cd2);
            Assert.AreEqual(cdt.Real, 2.2, Constants.ACCEPTABLE_ERROR);
            Assert.AreEqual(cdt.Imaginary, -1.5707963267949, Constants.ACCEPTABLE_ERROR);
            cdt = ComplexMath.Polar(cd3);
            Assert.AreEqual(cdt.Real, 1.1, Constants.ACCEPTABLE_ERROR);
            Assert.AreEqual(cdt.Imaginary, 0, Constants.ACCEPTABLE_ERROR);
            cdt = ComplexMath.Polar(cd4);
            Assert.AreEqual(cdt.Real, 2.45967477524977, Constants.ACCEPTABLE_ERROR);
            Assert.AreEqual(cdt.Imaginary, 2.0344439357957, Constants.ACCEPTABLE_ERROR);
        }
    }
}
